﻿using nplJobsBLL.ProcessSelectJobSeeker;
using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using nplJobsBLL.ProcessJobSeeker;
using Elmah;
using System.Text;
using System.Globalization;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Configuration;
using System.Web.Security;
using System.Web.Services;

public partial class JobSeeker_ViewProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (!Context.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");

            Label lblTitle = Page.Master.FindControl("lblTitle") != null ? (Label)Page.Master.FindControl("lblTitle") : new Label();
            lblTitle.Text = "Resume details";

            JobSeeker jobSeeker = new JobSeeker();
            //Guid jobseekerID = HttpContext.Current.Session["UserID"] != null ? Guid.Parse(HttpContext.Current.Session["UserID"].ToString()) : Guid.NewGuid();

            MembershipUser u = Membership.GetUser(User.Identity.Name);
            HttpContext.Current.Session["UserID"] = u != null ? u.ProviderUserKey : null;
            Guid jobseekerID = u.ProviderUserKey != null ? Guid.Parse(u.ProviderUserKey.ToString()) : Guid.Parse(HttpContext.Current.Session["UserID"].ToString());


            ProcessSelectJobSeeker jobSeekerProcess = new ProcessSelectJobSeeker();
            jobSeekerProcess.JobSeekerID = jobseekerID;
            jobSeekerProcess.invoke();
            jobSeeker = jobSeekerProcess.JobSeeker.FirstOrDefault();


            if (jobSeeker != null)
            {
                ResumeBrief(jobSeeker);
            }
            if (!IsPostBack)
                Page.Title = "npljobs.com || View Profile";
        }
        catch (System.Threading.ThreadAbortException)
        {

        } //
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }

    #region ResumeBrief
    private void ResumeBrief(JobSeeker jobSeeker)
    {
        string currentAge = string.Empty;
        try
        {
            if (jobSeeker != null)
            {
                string jobSeekerName = jobSeeker.FirstName + " " + jobSeeker.LastName;

                double d = (DateTime.Now - jobSeeker.Modifiedon).TotalDays;

                double result = 0;

                if (d > 0)
                {
                    if (d < 30)
                    {
                        result = d;
                        string _months = "Less than a month ago";
                        lblLastUpdated.Text = _months;
                        lblLastUpdatedOn.Text = "Last updated: " + _months;
                    }
                    else if (d > 30 && d < 365)
                    {
                        result = Math.Floor(d / 30);
                        string _months = result.ToString() + " month(s) ago";
                        lblLastUpdated.Text = _months;
                        lblLastUpdatedOn.Text = "Last updated: " + _months;
                    }
                    else if (d >= 365)
                    {
                        result = Math.Floor(d / 365);
                        string _years = result.ToString() + " year(s) ago";
                        lblLastUpdated.Text = _years;
                        lblLastUpdatedOn.Text = "Last updated: " + _years;
                    }
                }
                else
                {
                    int resultantDay = 0;
                    string days = string.Empty;

                    if (resultantDay == 0)
                        days = "Within last 24 hours";
                    else
                        days = resultantDay.ToString() + " day(s) ago";

                    lblLastUpdated.Text = days;
                    lblLastUpdatedOn.Text = "Last updated: " + days;
                }


                lblExpectedSalary.Text = !string.IsNullOrEmpty(jobSeeker.ExpectedSalaryRange) ? jobSeeker.ExpectedSalaryRange : "  N/A";
                lblPdfJobSeekerName.Text = lblJobSeekerName.Text = jobSeekerName;

                lblEmail.Text = !string.IsNullOrEmpty(jobSeeker.Email) ? jobSeeker.Email : jobSeeker.AlternativeEmail;
                lblMobile.Text = !string.IsNullOrEmpty(jobSeeker.MobilePhone) ? jobSeeker.MobilePhone : "N/A";
                lblAddress.Text = Server.HtmlEncode(jobSeeker.PostalAddress);
                lblHomePhone.Text = !string.IsNullOrEmpty(jobSeeker.HomePhone) ? jobSeeker.HomePhone : "N/A";

                List<EducationInfo> educationInfos = new List<EducationInfo>();
                List<ProfessionalInfo> professionalInfos = new List<ProfessionalInfo>();
                professionalInfos = GetProfessionalInfo(jobSeeker);

                int totalExperience = 0, months = 0;

                if (professionalInfos != null && professionalInfos.Count > 0)
                {
                    divExperienceHeading.Attributes.Add("style", string.Empty);

                    foreach (var p in professionalInfos)
                    {
                        int days = 0;

                        days = (p.ToDate - p.FromDate).Days > 0 ? (p.ToDate - p.FromDate).Days : 0;
                        months += days > 0 ? days / 30 : 0;
                    }

                    if (months > 12)
                    {
                        totalExperience = months / 12;
                        lblTotalExperience.Text = totalExperience.ToString() + " year(s)";
                    }
                    else
                    {
                        totalExperience = months;
                        lblTotalExperience.Text = totalExperience.ToString() + " month(s)";
                    }

                    ProfessionalInfo pf = professionalInfos.Find(pi => pi.JobSeekerID == jobSeeker.JobSeekerID && pi.CurrentPosition == true);
                    if (pf != null)
                    {
                        lblCompanyName.Text = pf != null ? pf.CompanyName : professionalInfos.FirstOrDefault().CompanyName;
                        lblCurrentJobTitle.Text = pf != null ? pf.Title : professionalInfos.FirstOrDefault().Title;
                    }
                    else
                    {
                        pf = professionalInfos.FirstOrDefault();
                        lblCompanyName.Text = pf != null ? pf.CompanyName : professionalInfos.FirstOrDefault().CompanyName;
                        lblCurrentJobTitle.Text = pf != null ? pf.Title : professionalInfos.FirstOrDefault().Title;
                    }
                }
                else
                {
                    divExperienceHeading.Attributes.Add("style", "display:none;");
                    lblTotalExperience.Text = "No experience(s) mentioned";

                }

                educationInfos = GetEducationInfo(jobSeeker);
                if (educationInfos != null && educationInfos.Count > 0)
                {
                    EducationInfo ef;
                    if (educationInfos != null && educationInfos.Count > 0)
                    {
                        divEducationHeadading.Attributes.Add("style", string.Empty);

                        ef = educationInfos.FirstOrDefault(ei => ei.JobSeekerID == jobSeeker.JobSeekerID);
                        if (ef != null)
                        {
                            lblInstitutionName.Text = ef != null ? ef.InstitutionName : educationInfos.FirstOrDefault().InstitutionName;
                            lblDegree.Text = ef != null ? ef.Degree : educationInfos.FirstOrDefault().Degree;
                        }
                        else
                        {
                            ef = educationInfos.FirstOrDefault();
                            if (ef != null)
                            {
                                lblInstitutionName.Text = ef != null ? ef.InstitutionName : educationInfos.FirstOrDefault().InstitutionName;
                                lblDegree.Text = ef != null ? ef.Degree : educationInfos.FirstOrDefault().Degree;
                            }

                        }
                    }
                }
                else
                {
                    divEducationHeadading.Attributes.Add("style", "display:none;");
                }

                if (professionalInfos != null && professionalInfos.Count > 0)
                {
                    LoadProfessionInfo(professionalInfos);
                }
                else
                {
                    professionalInfos = GetProfessionalInfo(jobSeeker);
                    LoadProfessionInfo(professionalInfos);
                }
                if (educationInfos != null && educationInfos.Count > 0)
                {
                    LoadEducation(educationInfos);
                }
                else
                {
                    educationInfos = GetEducationInfo(jobSeeker);
                    LoadEducation(educationInfos);
                }

                LoadSkills(jobSeeker);
                LoadLanguages(jobSeeker);
                LoadQualifications(jobSeeker);


                hlName.InnerText = jobSeekerName;
                plObjective.InnerText = jobSeeker.AboutMe;

                string postalAddrngess = string.Empty, remainingAddress = string.Empty, address = string.Empty;

                List<string> addressItems = new List<string>();
                if (!string.IsNullOrEmpty(jobSeeker.PostalAddress))
                {
                    addressItems.Add(jobSeeker.PostalAddress);
                }
                else
                {
                    if (!string.IsNullOrEmpty(jobSeeker.City))
                    {
                        addressItems.Add(jobSeeker.City);
                    }
                    if (!string.IsNullOrEmpty(jobSeeker.District))
                    {
                        addressItems.Add(jobSeeker.District);
                    }
                    if (!string.IsNullOrEmpty(jobSeeker.Country))
                    {
                        addressItems.Add(jobSeeker.Country);
                    }
                }
                address = string.Join(",", addressItems.ToArray());
                if (!string.IsNullOrEmpty(address))
                {
                    lblPostalAddress.Text = address + "<br>";
                }
                else
                {
                    lblPostalAddress.Visible = false;
                }
                //Gender- Male, Age-35, Marital Status- Married<br>

                string personalInfo = string.Empty;

                if (!string.IsNullOrEmpty(jobSeeker.Gender))
                {
                    personalInfo = "Gender : " + jobSeeker.Gender;
                }
                if (!string.IsNullOrEmpty(jobSeeker.DOB.ToString()))
                {
                    double days = (DateTime.Now - jobSeeker.DOB).TotalDays;
                    double age = 0;

                    if (days > 365)
                    {
                        age = days > 365 ? days / 365 : 0;
                        currentAge = Math.Floor(age).ToString();
                        personalInfo += ", Age : " + Math.Floor(age).ToString();
                    }
                }

                if (!string.IsNullOrEmpty(jobSeeker.MaritalStatus))
                {
                    personalInfo += ", Marital Status :  " + jobSeeker.MaritalStatus;
                }

                if (!string.IsNullOrEmpty(personalInfo))
                {
                    lblPersonalInfo.Text = personalInfo + "<br>";
                }
                else
                {
                    lblPersonalInfo.Visible = false;
                }
                if (!string.IsNullOrEmpty(jobSeeker.Nationality))
                {
                    lblNationality.Text = "Nationality - " + jobSeeker.Nationality + "<br>";
                }
                else
                {
                    lblNationality.Visible = false;
                }


                string imagePath = Helper.ResolveUrl("~/UploadedImage/" + jobSeeker.Photo);

                string _file = Server.MapPath(imagePath);
                if (string.IsNullOrEmpty(jobSeeker.Photo) || !File.Exists(_file))
                {
                    imgCandidate.Src = Helper.ResolveUrl("~/img/candidate-cv-photo.png");
                }
                else
                {
                    //string p = img + jobSeeker.Photo.ToString();
                    imgCandidate.Src = imagePath;
                }
                ///Code for PDF

                if (educationInfos != null)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (EducationInfo edu in educationInfos.OrderByDescending(edu => edu.FromDate))
                    {
                        string duration = EduDuration(edu);
                        StringBuilder sbEdu = new StringBuilder();
                        sbEdu.Append(edu.InstitutionName);
                        sbEdu.Append("<br/>&nbsp;&nbsp;&nbsp;");
                        sbEdu.Append(edu.Degree);
                        sbEdu.Append("<br/>&nbsp;&nbsp;&nbsp;");
                        sbEdu.Append(duration);
                        sbEdu.Append("<br/><br/>");
                        sb.Append(string.Format("<li style='margin-left: -10px !important;'>{0}</li>", sbEdu.ToString()));
                    }
                    lblPdfEducation.Visible = true;
                    lblPdfEducation.Text = sb.ToString();
                }
                else
                {
                    trEducationTitle.Visible = false;
                    trEducationText.Visible = false;
                    lblPdfEducation.Visible = false;
                }

                if (professionalInfos != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var pi in professionalInfos.OrderBy(pi => pi.CurrentPosition ? 0 : 1))
                    {
                        string duration = string.Empty;

                        string startTime = GetMonth(pi.FromDate.Month) + " " + pi.FromDate.Year.ToString();
                        string endTime = GetMonth(pi.ToDate.Month) + " " + pi.ToDate.Year.ToString();
                        duration = startTime + " - " + endTime;

                        int expInMonths = (pi.ToDate - pi.FromDate).Days / 30;

                        string expInMonthsStr = string.Empty;

                        if (expInMonths == 0)
                        {
                            expInMonthsStr = "Less than a month";

                        }
                        else if (expInMonths > 12)
                        {
                            int _expInMonths = expInMonths / 12;
                            expInMonthsStr = _expInMonths.ToString() + " Year(s)";
                        }
                        else
                        {
                            int _expInMonths = expInMonths;
                            expInMonthsStr = _expInMonths.ToString() + " Month(s)";
                        }

                        StringBuilder sbPro = new StringBuilder();
                        sbPro.Append(pi.CompanyName);
                        sbPro.Append("<br/>");
                        sbPro.Append(pi.Title);
                        sbPro.Append("<br/>");
                        sbPro.Append(duration + " (" + expInMonthsStr + ")");
                        sbPro.Append("<br/>");
                        sbPro.Append(pi.PositionDescription);
                        sbPro.Append("<br/><br/>");
                        sb.Append(string.Format("<li style='margin-left: -10px !important;'>{0}</li>", sbPro.ToString()));

                    }
                    lblPdfExperience1.Text = sb.ToString();
                }
                else
                {
                }


                string[] skills = !string.IsNullOrEmpty(jobSeeker.Skills) ? jobSeeker.Skills.Split(new char[] { ',' }) : null;

                if (skills != null && skills.Any())
                {
                    //foreach (string skill in skills.Take(10).ToList())
                    //{
                    //    sbSkills.Append(string.Format("<li style='margin-left: -10px !important;'>{0}</li>", skill));
                    //}
                    //lblPdfSkill1.Visible = true;
                    //lblPdfSkill1.Text = sbSkills.ToString();

                    lblPdfSkill.Visible = true;
                    lblPdfSkill.Text = string.Join(",", skills);
                }
                else
                {
                    trSkillsTitle.Visible = false;
                    trSkillsText.Visible = false;
                }
                List<JobSeekerLanguageProficiency> lstJobSeekerLanguageProficiency = LoadLanguages(jobSeeker);
                if (lstJobSeekerLanguageProficiency != null && lstJobSeekerLanguageProficiency.Any())
                {
                    StringBuilder sbLanguages = new StringBuilder();

                    foreach (var lp in lstJobSeekerLanguageProficiency.Where(ln => ln.ProficiencyID > 0))
                    {
                        sbLanguages.Append(string.Format("<li style='margin-left: -10px !important;'>{0}</li>", lp.Language + " - " + lp.Proficiency));
                    }
                    lblPdfLanguages.Text = sbLanguages.ToString();
                }
                lblPdfObjective.Text = jobSeeker.AboutMe;
                lblPdfEmailAddress.Text = !string.IsNullOrEmpty(jobSeeker.Email) ? jobSeeker.Email : jobSeeker.AlternativeEmail;
                lblPdfMobilePhone.Text = !string.IsNullOrEmpty(jobSeeker.MobilePhone) ? jobSeeker.MobilePhone : "N/A";
                lblPdfPhone.Text = !string.IsNullOrEmpty(jobSeeker.HomePhone) ? jobSeeker.HomePhone : "N/A";
                lblPdfDesiredSalary.Text = !string.IsNullOrEmpty(jobSeeker.ExpectedSalaryRange) ? jobSeeker.ExpectedSalaryRange : "N/A";
                lblPdfPostalAddress.Text = Server.HtmlEncode(jobSeeker.PostalAddress);
                lblPdfNationality.Text = !string.IsNullOrEmpty(jobSeeker.Nationality) ? jobSeeker.Nationality : "N/A";
                lblPdfAge.Text = !string.IsNullOrEmpty(currentAge) ? currentAge : "N/A";
                lblGender.Text = !string.IsNullOrEmpty(jobSeeker.Gender) ? jobSeeker.Gender : "N/A";
                lblMaritalStatus.Text = !string.IsNullOrEmpty(jobSeeker.MaritalStatus) ? jobSeeker.MaritalStatus : "N/A";

            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }
    #endregion

    #region LoadQualifications
    private void LoadQualifications(JobSeeker jobSeeker)
    {
        string[] qualifications = jobSeeker.Qualifications != null ? jobSeeker.Qualifications.ToString().Split(new char[] { '~' }, StringSplitOptions.RemoveEmptyEntries).ToArray() : null;

        StringBuilder sbQualifications = new StringBuilder();

        if (qualifications != null && qualifications.Count() > 0)
        {
            sbQualifications.Append("<ul class=\"skill-list\">");

            foreach (var qa in qualifications)
            {
                sbQualifications.Append("<li><span>" + qa.ToString().Trim() + "</span></li>");
            }
            sbQualifications.Append("</ul>");
        }

        if (sbQualifications != null)
        {
            divQualifications.InnerHtml = sbQualifications.ToString();
        }
    }
    #endregion

    #region LoadLanguages
    private List<JobSeekerLanguageProficiency> LoadLanguages(JobSeeker jobSeeker)
    {
        List<JobSeekerLanguageProficiency> lstJobSeekerLanguageProficiency = new List<JobSeekerLanguageProficiency>();

        try
        {

            ProcessSelectJobSeekerLanguageProficiency jobSeekerLanguageProficiency = new ProcessSelectJobSeekerLanguageProficiency();
            jobSeekerLanguageProficiency.JobSeekerID = jobSeeker.JobSeekerID;
            jobSeekerLanguageProficiency.invoke();
            lstJobSeekerLanguageProficiency = jobSeekerLanguageProficiency.JobSeekerLanguageProficiency;

            StringBuilder sbLanguages = new StringBuilder();

            if (lstJobSeekerLanguageProficiency != null && lstJobSeekerLanguageProficiency.Count > 0)
            {
                sbLanguages.Append("<ul class=\"skill-list\">");

                if (lstJobSeekerLanguageProficiency != null && lstJobSeekerLanguageProficiency.Count() > 0)
                {
                    foreach (var lp in lstJobSeekerLanguageProficiency.Where(ln => ln.ProficiencyID > 0))
                    {
                        sbLanguages.Append("<li><span>" + lp.Language.Trim() + " - " + lp.Proficiency + "</span></li>");
                    }
                }
                sbLanguages.Append("</ul>");
            }

            if (sbLanguages != null)
            {
                divLanguages.InnerHtml = sbLanguages.ToString();
            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }

        return lstJobSeekerLanguageProficiency;
    }
    #endregion

    #region LoadSkills
    /// <summary>
    /// This is a quick change this code will have to be changed with a better implementation
    /// </summary>
    /// <param name="jobSeeker"></param>
    private void LoadSkills(JobSeeker jobSeeker)
    {
        if (jobSeeker != null)
        {
            string[] skills = !string.IsNullOrEmpty(jobSeeker.Skills) ? jobSeeker.Skills.Split(new char[] { ',' }) : null;

            if (skills != null && skills.Any())
            {

                divAllSkills.Visible = true;
                int count = 0;

                StringBuilder sbSkills = new StringBuilder();
                StringBuilder sbAllSkills = new StringBuilder();
                sbAllSkills.Append("<div class=\"col-md-10\">");

                //foreach (var skill in skills)
                //{
                //    count = count + 1;

                //    if (count == 1)
                //    {
                //        sbSkills = new StringBuilder();
                //        //sbSkills.Append("<div class=\"col-md-4\">");
                //        //sbSkills.Append("<ul class=\"skill-list\">");
                //    }
                //    if (count == skills.Count())
                //    {
                //        if (count == skills.Count())
                //        {
                //            sbSkills.Append("<li><span>" + skill + "</span></li>");

                //            //sbSkills.Append(" </ul>");
                //            //sbSkills.Append(" </div>");
                //            sbAllSkills.Append(sbSkills.ToString());
                //        }
                //    }
                //    else
                //    {
                //        sbSkills.Append("<li><span>" + skill + "</span></li>");
                //    }
                //}

                skills.ToList().Sort();
                count = skills.Count();

                if (count <= 20)
                {
                    List<string> skillsSet = skills.ToList().GetRange(0, count - 1);

                    if (skillsSet.Any())
                    {
                        GetSkill(sbSkills, skillsSet);
                    }
                }
                else if (count > 20 && count <= 40)
                {
                    List<string> skillsSet = new List<string>();
                    skillsSet = skills.ToList().GetRange(0, 19);
                    if (skillsSet.Any())
                    {
                        GetSkill(sbSkills, skillsSet);
                    }
                    skillsSet = new List<string>();
                    skillsSet = skills.ToList().GetRange(20, 19);
                    if (skillsSet.Any())
                    {
                        GetSkill(sbSkills, skillsSet);
                    }
                }
                else if (count > 40)
                {
                    List<string> skillsSet = new List<string>();
                    skillsSet = skills.ToList().GetRange(0, 19);
                    if (skillsSet.Any())
                    {
                        GetSkill(sbSkills, skillsSet);
                    }
                    skillsSet = new List<string>();
                    skillsSet = skills.ToList().GetRange(20, 19);
                    if (skillsSet.Any())
                    {
                        GetSkill(sbSkills, skillsSet);
                    }
                    skillsSet = new List<string>();
                    int tCount = skills.Count() - 40;
                    skillsSet = skills.ToList().GetRange(40, tCount);
                    if (skillsSet.Any())
                    {
                        GetSkill(sbSkills, skillsSet);
                    }
                }

                sbAllSkills.Append(sbSkills);

                sbAllSkills.Append(" </div>");

                if (sbAllSkills != null)
                {
                    divSkills.InnerHtml = sbAllSkills.ToString();
                }
            }
            else
            {
                divAllSkills.Visible = false;
            }
        }
    }

    private static void GetSkill(StringBuilder sbSkills, List<string> skillsSet)
    {
        sbSkills.Append("<div class=\"col-md-4\">");
        sbSkills.Append("<ul class=\"skill-list\">");

        foreach (var skill in skillsSet)
        {
            sbSkills.Append("<li><span>" + skill.Trim() + "</span></li>");
        }
        sbSkills.Append(" </ul>");
        sbSkills.Append(" </div>");
    }

    //private void SkillsSetTwo(List<string> skills, StringBuilder sbSkills)
    //{
    //    foreach (var skill in skills)
    //    {
    //        sbSkills.Append("<li><span>" + skill + "</span></li>");
    //    }

    //    if (!string.IsNullOrEmpty(sbSkills.ToString()))
    //    {
    //        divSkillsSet2.Attributes.Add("display", "");
    //        ulSkillsSet2.InnerHtml = sbSkills.ToString();
    //    }
    //}


    #endregion

    #region LoadProfessionInfo
    private void LoadProfessionInfo(List<ProfessionalInfo> professionalInfo)
    {
        StringBuilder sbProfessionalInfo = new StringBuilder();

        List<ProfessionalInfo> professionInfoOrdered = new List<ProfessionalInfo>();

        if (professionalInfo != null)
        {
            foreach (ProfessionalInfo pi in professionalInfo.OrderBy(p => p.CurrentPosition ? 0 : 1))
            {
                string duration = string.Empty;
                string startTime = GetMonth(pi.FromDate.Month) + " " + pi.FromDate.Year.ToString();
                string endTime = GetMonth(pi.ToDate.Month) + " " + pi.ToDate.Year.ToString();
                duration = startTime + " - " + endTime;
                sbProfessionalInfo.Append("<div class=\"col-md-2\"> <span>" + duration + " </span></div>");
                sbProfessionalInfo.Append("<div class=\"col-md-10\"><p><strong>" + pi.CompanyName + "</strong><br> <span>" + pi.Title + "</span></p><p>" + pi.PositionDescription + "</p></div>");

                if (sbProfessionalInfo != null)
                {
                    divProfessionalInfo.InnerHtml = sbProfessionalInfo.ToString();
                }
            }
        }
    }
    #endregion

    #region LoadEducation
    private void LoadEducation(List<EducationInfo> educationInfo)
    {
        StringBuilder sbEducationInfo = new StringBuilder();

        List<EducationInfo> educationInfoOrdered = new List<EducationInfo>();

        //bool isAtTopDefined = educationInfo.Exists(ei => ei.DisplayAtTop);

        //if (isAtTopDefined)
        //{
        //    educationInfoOrdered = educationInfo.OrderBy(ei => ei.DisplayAtTop).ToList();
        //}
        //else
        //{
        //    educationInfoOrdered = educationInfo.OrderBy(ei => ei.FromDate).ToList();
        //}

        if (educationInfo != null)
        {
            foreach (EducationInfo ed in educationInfo.OrderByDescending(e => e.FromDate))
            {
                string duration = EduDuration(ed);
                sbEducationInfo.Append("<div class=\"col-md-2\"> <span>" + duration.Trim() + " </span></div>");
                sbEducationInfo.Append("<div class=\"col-md-10\"><p><strong>" + ed.InstitutionName.Trim() + "</strong><br> <span>" + ed.Degree.Trim() + "</span></p></div>");

                if (sbEducationInfo != null)
                {
                    divEducationInfo.InnerHtml = sbEducationInfo.ToString();
                }

            }
        }
    }

    private string EduDuration(EducationInfo ed)
    {
        DateTime startDate, endDate;

        DateTime.TryParse(ed.FromDate.ToString(), out startDate);
        DateTime.TryParse(ed.ToDate.ToString(), out endDate);

        string duration = string.Empty;
        string startTime = GetMonth(startDate.Month) + " " + startDate.Year.ToString();
        string endTime = GetMonth(endDate.Month) + " " + endDate.Year.ToString();
        duration = startTime + " - " + endTime;
        return duration;
    }
    #endregion

    #region GetProfessionalInfo
    private List<ProfessionalInfo> GetProfessionalInfo(JobSeeker jobSeeker)
    {
        List<ProfessionalInfo> professionalInfo = new List<ProfessionalInfo>();

        ProcessSelectProfessionalInfo selectProInfo = new ProcessSelectProfessionalInfo();
        selectProInfo.ProfessionalInfoID = -1;
        selectProInfo.JobseekerID = jobSeeker.JobSeekerID;
        selectProInfo.invoke();
        professionalInfo = selectProInfo.ProfessionalInfo;

        return professionalInfo;
    }
    #endregion

    #region GetEducationInfo
    private List<EducationInfo> GetEducationInfo(JobSeeker jobSeeker)
    {
        List<EducationInfo> educationInfos = new List<EducationInfo>();

        ProcessSelectEducationalInfo selectEducationInfo = new ProcessSelectEducationalInfo();
        selectEducationInfo.EducationalInfoID = -1;
        selectEducationInfo.JobseekerID = jobSeeker.JobSeekerID;
        selectEducationInfo.invoke();
        educationInfos = selectEducationInfo.EducationalInfo;

        List<EducationInfo> _educationInfos = educationInfos != null ? educationInfos.OrderByDescending(edu => edu.FromDate).ToList() : new List<EducationInfo>();
        return _educationInfos;
    }
    #endregion

    #region GetMonth
    private string GetMonth(int m)
    {
        string month = string.Empty;

        if (m > 0)
        {
            string mon = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m);
            if (!string.IsNullOrEmpty(mon))
            {
                month = mon.Substring(0, 3);
            }
        }
        return month;
    }
    #endregion


    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Resume.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            jobseekerProfile.RenderControl(hw);

            string path = Server.MapPath("~/img/candidate-cv-photo.png");
            string str = sw.ToString().Replace("../img/candidate-cv-photo.png", path).Replace("/img/candidate-cv-photo.png", path);

            StringReader sr = new StringReader(str.ToString());
            Document pdfDoc = new Document(PageSize.A4, 50f, 30f, 50f, 30f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter docWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            PdfWriterEvents writerEvent = new PdfWriterEvents("www.npljobs.com");
            docWriter.PageEvent = writerEvent;
            pdfDoc.Open();
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();


        }
        catch (System.Threading.ThreadAbortException)
        {

            //ErrorSignal.FromCurrentContext().Raise(ex);
        } //
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }

    protected void btnExport_OnClick(object sender, EventArgs e)
    {

    }
}